﻿// -------------------------------------------------------------------------
//    @FileName         :    NFTransOpStoreageBag.cpp
//    @Author           :    Gao.Yi
//    @Date             :    2025-10-20
//    @Email			:    445267987@qq.com
//    @Module           :    NFTransOpStoreageBag
//    @Desc             :
//
// -------------------------------------------------------------------------

#include "NFTransOpStoreageBag.h"

#include "NFLogicCommon/BagPage/NFBagPageGrid.h"
#include "NFLogicCommon/BagPage/NFBagPageOp.h"
#include "Bag/NFBagPageOpMgr.h"
#include "NFPacketSendUtil.h"
#include "NFPlayer.h"
#include "proto_cs_cmd.nanopb.h"
#include "proto_svr_bag.pb.h"
#include "proto_svr_common.nanopb.h"
#include "DescStore/BagDesc.h"

enum {
    EN_OP_BUY_BAG_GRID_PRE = 1,
    EN_OP_BUY_BAG_GRID_COST = 2,
    EN_OP_STORAGE_SAVE_PRE = 3,
    EN_OP_STORAGE_SAVE_COST = 4,
    EN_OP_PICK_FROM_STORAGE_PRE = 5,
    EN_OP_PICK_FROM_STORAGE_COST = 6,
};

NFTransOpStoreageBag::NFTransOpStoreageBag()
{
    if (EN_OBJ_MODE_INIT == NFShmMgr::Instance()->GetCreateMode())
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFTransOpStoreageBag::~NFTransOpStoreageBag()
{
}

int NFTransOpStoreageBag::CreateInit()
{
    m_iBagPage = 0;
    m_iBuyTimes = 0;
    m_iItemId = 0;
    m_iItemNum = 0;
    m_iItemUUID = 0;
    m_iGridIndex = INVALID_ID;
    return 0;
}

int NFTransOpStoreageBag::ResumeInit()
{
    return 0;
}

int NFTransOpStoreageBag::HandleCSMsgReq(const google::protobuf::Message* pCsMsgReq)
{
    int iRetCode = 0;
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);
    switch (m_stCSMsgHeadReq.m_iMsgID)
    {
        case proto_ff::E_CS_BUY_BAG_GRID_REQ:
        {
            iRetCode = HandleBuyBagGridReq(dynamic_cast<const proto_ff::Proto_CSBuyBagGridInfo*>(pCsMsgReq));
            break;
        }
        case proto_ff::E_CS_STORAGE_SAVE_REQ:
        {
            iRetCode = OnHandleStorageSaveReq(dynamic_cast<const proto_ff::Proto_CSStorageMoveReq*>(pCsMsgReq));
            break;
        }
        case proto_ff::E_CS_STORAGE_PICK_REQ:
        {
            iRetCode = OnHandlePickFromStorageReq(dynamic_cast<const proto_ff::Proto_CSStorageMoveReq*>(pCsMsgReq));
            break;
        }
        default:
        {
            iRetCode = -1;
            LOG_ERR(pPlayer->GetRoleID(), -1, "HandleCSMsgReq nMsgID:{}", m_stCSMsgHeadReq.m_iMsgID);
            break;
        }
    }

    if (iRetCode != 0)
    {
        SetFinished(iRetCode);
        return 0;
    }
    return 0;
}

int NFTransOpStoreageBag::HandleBuyBagGridReq(const proto_ff::Proto_CSBuyBagGridInfo* pReq)
{
    int iRetCode = 0;
    CHECK_NULL(0, pReq);
    m_iBagPage = pReq->page();
    m_iBuyTimes = pReq->times();
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    auto pBagDesc = BagDesc::Instance()->GetDescByTypeTimes(m_iBagPage, m_iBuyTimes);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pBagDesc, "page:{} times:{}", m_iBagPage, m_iBuyTimes);

    NFBagPageOpMgr opTestMgr(pPlayer, proto_ff::EN_ICR_BUY_BAG_GRID, false);
    iRetCode = opTestMgr.DelItems(pBagDesc->use_item_id, pBagDesc->use_item_num);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "page:{} times:{} use_item_id:{} use_item_num:{}", m_iBagPage, m_iBuyTimes, pBagDesc->use_item_id, pBagDesc->use_item_num);

    pPlayer->SendTransToOnlineServer(E_SS_ONLINE_ROLE_BUY_BAG_GRID_PRE,  *pReq, GetGlobalId(), 0);
    SetState(EN_OP_BUY_BAG_GRID_PRE);
    return 0;
}

int NFTransOpStoreageBag::HandleDispSvrRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    switch (nMsgId)
    {
        case E_SS_ONLINE_ROLE_BUY_BAG_GRID_PRE:
        {
            iRetCode = HandleBuyBagGridPreRes(nMsgId, packet, reqTransId, rspTransId);
            break;
        }
        case E_SS_ONLINE_ROLE_BUY_BAG_GRID:
        {
            iRetCode = HandleBuyBagGridRes(nMsgId, packet, reqTransId, rspTransId);
            break;
        }
        case E_SS_ONLINE_ROLE_STORAGE_SAVE_PRE:
        {
            iRetCode = HandleStorageSavePreRes(nMsgId, packet, reqTransId, rspTransId);
            break;
        }
        case E_SS_ONLINE_ROLE_STORAGE_SAVE:
        {
            iRetCode = HandleStorageSaveRes(nMsgId, packet, reqTransId, rspTransId);
            break;
        }
        case E_SS_ONLINE_ROLE_PICK_FROM_STORAGE_PRE:
        {
            iRetCode = HandleStoragePickStoragePreRes(nMsgId, packet, reqTransId, rspTransId);
            break;
        }
        case E_SS_ONLINE_ROLE_PICK_FROM_STORAGE:
        {
            iRetCode = HandleStoragePickStorageRes(nMsgId, packet, reqTransId, rspTransId);
            break;
        }
        default:
        {
            iRetCode = -1;
            LOG_ERR(GetPlayer()->GetRoleID(), -1, "HandleDispSvrRes nMsgId:{}", nMsgId);
            break;
        }
    }

    if (iRetCode != 0)
    {
        SetFinished(iRetCode);
        return iRetCode;
    }
    return 0;
}

int NFTransOpStoreageBag::HandleBuyBagGridPreRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    CHECK_EXPR(GetState() == EN_OP_BUY_BAG_GRID_PRE, -1, "HandleBuyBagGridRes GetState:{} != EN_OP_BUY_BAG_GRID_PRE", GetState());

    proto_ff::Proto_OnlineToLogicBuyBagGridPreRsp rsp;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, rsp);

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (rsp.result() != 0)
    {
        LOG_ERR(pPlayer->GetRoleID(), rsp.result(), "HandleBuyBagGridRes result:{}", rsp.result());
        SetFinished(rsp.result());
        return 0;
    }

    auto pBagDesc = BagDesc::Instance()->GetDescByTypeTimes(m_iBagPage, m_iBuyTimes);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pBagDesc, "page:{} times:{}", m_iBagPage, m_iBuyTimes);

    NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_BUY_BAG_GRID);
    iRetCode = opMgr.DelItems(pBagDesc->use_item_id, pBagDesc->use_item_num);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "page:{} times:{} use_item_id:{} use_item_num:{}",m_iBagPage, m_iBuyTimes, pBagDesc->use_item_id, pBagDesc->use_item_num);
    iRetCode = opMgr.Commit(true);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "opMgr.Commit failed");

    proto_ff::Proto_OnlineToLogicBuyBagGridReq req;
    req.mutable_info()->set_page(m_iBagPage);
    req.mutable_info()->set_times(m_iBuyTimes);
    pPlayer->SendTransToOnlineServer(E_SS_ONLINE_ROLE_BUY_BAG_GRID,  req, GetGlobalId(), 0);
    SetState(EN_OP_BUY_BAG_GRID_COST);
    return 0;
}

int NFTransOpStoreageBag::HandleBuyBagGridRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    CHECK_EXPR(GetState() == EN_OP_BUY_BAG_GRID_COST, -1, "HandleBuyBagGridRes GetState:{} != EN_OP_BUY_BAG_GRID_COST", GetState());

    proto_ff::Proto_OnlineToLogicBuyBagGridRsp rsp;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, rsp);

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (rsp.result() != 0)
    {
        auto pBagDesc = BagDesc::Instance()->GetDescByTypeTimes(m_iBagPage, m_iBuyTimes);
        CHECK_NULL_WF(pPlayer->GetRoleID(), pBagDesc, "page:{} times:{}", m_iBagPage, m_iBuyTimes);

        NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_BUY_BAG_GRID);
        iRetCode = opMgr.AddToBag(pBagDesc->use_item_id, pBagDesc->use_item_num);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "page:{} times:{} use_item_id:{} use_item_num:{}",m_iBagPage, m_iBuyTimes, pBagDesc->use_item_id, pBagDesc->use_item_num);
        iRetCode = opMgr.Commit(true);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "opMgr.Commit failed");

        LOG_ERR(pPlayer->GetRoleID(), rsp.result(), "HandleBuyBagGridRes result:{}, must return coin", rsp.result());
        SetFinished(rsp.result());
        return 0;
    }

    SetFinished(0);
    return 0;
}

int NFTransOpStoreageBag::HandleTransFinished(int iRunLogicRetCode)
{
    if (iRunLogicRetCode != 0)
    {
        auto pPlayer = GetPlayer();
        CHECK_NULL(0, pPlayer);
        NFPacketSendUtil::SendErrToClient(pPlayer, m_stCSMsgHeadReq.m_iMsgID, iRunLogicRetCode);
    }

    return 0;
}

int NFTransOpStoreageBag::OnHandleStorageSaveReq(const proto_ff::Proto_CSStorageMoveReq* pReq)
{
    int iRetCode = 0;
    CHECK_NULL(0, pReq);
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);
    m_iBagPage = pReq->storage_page();
    m_iItemId = pReq->item_id();
    m_iItemNum = pReq->item_num();
    m_iItemUUID = pReq->uuid();
    m_iGridIndex = pReq->grid_index();

    auto pItemDesc = ItemDesc::Instance()->GetDesc(m_iItemId);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pItemDesc, "item_id:{}", m_iItemId);

    NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_STORAGE_OP, false);

    NFBagPageOp* pBagPageOp = opMgr.GetBagPageOpByBagType(m_iBagPage);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pBagPageOp, "GetBagPageOpByBagType, iBagType:{} iGridIndex:{}", m_iBagPage, m_iGridIndex);

    auto pGrid = pBagPageOp->GetGrid(m_iGridIndex);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pBagPageOp, "GetGrid, iBagType:{} iGridIndex:{}", m_iBagPage, m_iGridIndex);

    if (pGrid->GetItemID() != m_iItemId || pGrid->GetUUID() !=m_iItemUUID || m_iItemNum <= 0)
    {
        LOG_ERR(pPlayer->GetRoleID(), -1, "OnHandleStoreageSaveReq, pGrid->GetItemID():{} pGrid->GetUUID():{} item_id:{} uuid:{}", pGrid->GetItemID(), pGrid->GetUUID(), m_iItemId, m_iItemUUID);
        return ERR_CODE_INVALID_REQ_PARAM;
    }

    if (pGrid->GetItem().IsBind())
    {
        LOG_ERR(pPlayer->GetRoleID(), -1, "OnHandleStoreageSaveReq, item is bind. {}", pGrid->GetItem().ShortDebugString());
        return ERR_CODE_INVALID_REQ_PARAM;
    }

    NFGridItem saveItem = pGrid->GetItem();
    iRetCode = pBagPageOp->DelItemByGridWithNum(*pGrid, m_iItemNum);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "DelItemByGridWithNum, iBagType:{} iGridIndex:{} item_id:{} item_num:{}",m_iBagPage, m_iGridIndex, m_iItemId, m_iItemUUID);
    saveItem.SetNumber(m_iItemNum);

    if (pItemDesc->save_storage_cost_id > 0)
    {
        iRetCode = opMgr.DelItems(pItemDesc->save_storage_cost_id, pItemDesc->save_storage_cost_num * m_iItemNum);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "DelItems, item_id:{} item_num:{}", pItemDesc->save_storage_cost_id, pItemDesc->save_storage_cost_num *m_iItemNum);
    }

    proto_ff::Proto_OnlineToLogicStorageSavePreReq preReq;
    saveItem.ToPb(preReq.mutable_save_item());
    pPlayer->SendTransToOnlineServer(E_SS_ONLINE_ROLE_STORAGE_SAVE_PRE, preReq, GetGlobalId(), 0);
    SetState(EN_OP_STORAGE_SAVE_PRE);
    return 0;
}

int NFTransOpStoreageBag::HandleStorageSavePreRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    CHECK_EXPR(GetState() == EN_OP_STORAGE_SAVE_PRE, -1, "HandleStorageSavePreRes GetState:{} != EN_OP_STORAGE_SAVE_PRE", GetState());

    proto_ff::Proto_OnlineToLogicStorageSavePreRsp xData;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, xData);

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (xData.result() != 0)
    {
        LOG_ERR(pPlayer->GetRoleID(), xData.result(), "HandleStorageSavePreRes result:{}", xData.result());
        SetFinished(xData.result());
        return 0;
    }

    auto pItemDesc = ItemDesc::Instance()->GetDesc(m_iItemId);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pItemDesc, "item_id:{}", m_iItemId);

    NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_STORAGE_OP, true);

    NFBagPageOp* pBagPageOp = opMgr.GetBagPageOpByBagType(m_iBagPage);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pBagPageOp, "GetBagPageOpByBagType, iBagType:{} iGridIndex:{}", m_iBagPage, m_iGridIndex);

    auto pGrid = pBagPageOp->GetGrid(m_iGridIndex);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pBagPageOp, "GetGrid, iBagType:{} iGridIndex:{}", m_iBagPage, m_iGridIndex);

    if (pGrid->GetItemID() != m_iItemId || pGrid->GetUUID() !=m_iItemUUID || m_iItemNum <= 0)
    {
        LOG_ERR(pPlayer->GetRoleID(), -1, "OnHandleStoreageSaveReq, pGrid->GetItemID():{} pGrid->GetUUID():{} item_id:{} uuid:{}", pGrid->GetItemID(), pGrid->GetUUID(), m_iItemId, m_iItemUUID);
        return ERR_CODE_INVALID_REQ_PARAM;
    }

    m_stItem = pGrid->GetItem();
    iRetCode = pBagPageOp->DelItemByGridWithNum(*pGrid, m_iItemNum);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "DelItemByGridWithNum, iBagType:{} iGridIndex:{} item_id:{} item_num:{}",m_iBagPage, m_iGridIndex, m_iItemId, m_iItemUUID);
    m_stItem.SetNumber(m_iItemNum);

    if (pItemDesc->save_storage_cost_id > 0)
    {
        iRetCode = opMgr.DelItems(pItemDesc->save_storage_cost_id, pItemDesc->save_storage_cost_num * m_iItemNum);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "DelItems, item_id:{} item_num:{}", pItemDesc->save_storage_cost_id, pItemDesc->save_storage_cost_num *m_iItemNum);
    }

    iRetCode = opMgr.Commit(true);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "Commit");

    proto_ff::Proto_OnlineToLogicStorageSaveReq req;
    m_stItem.ToPb(req.mutable_save_item());
    pPlayer->SendTransToOnlineServer(E_SS_ONLINE_ROLE_STORAGE_SAVE, req, GetGlobalId(), 0);
    SetState(EN_OP_STORAGE_SAVE_COST);
    return 0;
}

int NFTransOpStoreageBag::HandleStorageSaveRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    CHECK_EXPR(GetState() == EN_OP_STORAGE_SAVE_COST, -1, "HandleStorageSaveRes GetState:{} != EN_OP_STORAGE_SAVE_PRE", GetState());

    proto_ff::Proto_OnlineToLogicStorageSaveRsp xData;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, xData);

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (xData.result() != 0)
    {
        auto pItemDesc = ItemDesc::Instance()->GetDesc(m_iItemId);
        CHECK_NULL_WF(pPlayer->GetRoleID(), pItemDesc, "item_id:{}", m_iItemId);

        NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_STORAGE_OP, true);

        NFBagPageOp* pBagPageOp = opMgr.GetBagPageOpByBagType(m_iBagPage);
        CHECK_NULL_WF(pPlayer->GetRoleID(), pBagPageOp, "GetBagPageOpByBagType, iBagType:{} iGridIndex:{}", m_iBagPage, m_iGridIndex);

        int iAddNum = 0;
        iRetCode = pBagPageOp->AddItem(&m_stItem, iAddNum);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "AddItem, m_stItem:{}", m_stItem.ShortDebugString());

        if (iAddNum != m_iItemNum)
        {
            CHECK_ERR(pPlayer->GetRoleID(), iAddNum, "AddItem, iAddNum:{} m_stItem:{}", iAddNum, m_stItem.ShortDebugString());
        }

        if (pItemDesc->save_storage_cost_id > 0)
        {
            iRetCode = opMgr.AddToBag(pItemDesc->save_storage_cost_id, pItemDesc->save_storage_cost_num * m_iItemNum);
            CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "AddToBag, item_id:{} item_num:{}", pItemDesc->save_storage_cost_id, pItemDesc->save_storage_cost_num *m_iItemNum);
        }

        iRetCode = opMgr.Commit(true);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "Commit");

        LOG_ERR(pPlayer->GetRoleID(), xData.result(), "HandleStorageSaveRes result:{}", xData.result());
        SetFinished(xData.result());
        return 0;
    }

    proto_ff::Proto_CSRsp respond;
    proto_ff::Proto_CSStorageMoveRsp* rsp = respond.mutable_storage_move_pick_resp();
    rsp->set_storage_page(m_iBagPage);
    rsp->set_grid_index(m_iGridIndex);
    NFPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_STORAGE_SAVE_RSP, &respond);
    SetFinished(0);
    return 0;
}

int NFTransOpStoreageBag::OnHandlePickFromStorageReq(const proto_ff::Proto_CSStorageMoveReq* pReq)
{
    int iRetCode = 0;
    CHECK_NULL(0, pReq);
    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);
    m_iBagPage = pReq->storage_page();
    m_iItemId = pReq->item_id();
    m_iItemNum = pReq->item_num();
    m_iItemUUID = pReq->uuid();
    m_iGridIndex = pReq->grid_index();

    auto pItemDesc = ItemDesc::Instance()->GetDesc(m_iItemId);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pItemDesc, "item_id:{}", m_iItemId);

    proto_ff::Proto_OnlineToLogicPickFromStoragePreReq preReq;
    *preReq.mutable_req() = *pReq;
    pPlayer->SendTransToOnlineServer(E_SS_ONLINE_ROLE_PICK_FROM_STORAGE_PRE, preReq, GetGlobalId(), 0);
    SetState(EN_OP_PICK_FROM_STORAGE_PRE);
    return 0;
}

int NFTransOpStoreageBag::HandleStoragePickStoragePreRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
    int iRetCode = 0;
    CHECK_EXPR(GetState() == EN_OP_PICK_FROM_STORAGE_PRE, -1, "HandleStoragePickStoragePreRes GetState:{} != EN_OP_PICK_FROM_STORAGE_PRE", GetState());

    proto_ff::Proto_OnlineToLogicPickFromStoragePreRsp xData;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, xData);

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (xData.result() != 0)
    {
        LOG_ERR(pPlayer->GetRoleID(), xData.result(), "HandleStoragePickStoragePreRes result:{}", xData.result());
        SetFinished(xData.result());
        return 0;
    }

    auto pItemDesc = ItemDesc::Instance()->GetDesc(m_iItemId);
    CHECK_NULL_WF(pPlayer->GetRoleID(), pItemDesc, "item_id:{}", m_iItemId);

    NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_STORAGE_OP, true);

    m_stItem.FromPb(xData.item());
    if (m_stItem.GetItemID() != m_iItemId || m_stItem.GetNumber() != m_iItemNum || m_stItem.GetUUID() != m_iItemUUID)
    {
        LOG_ERR(pPlayer->GetRoleID(), -1, "HandleStoragePickStoragePreRes m_stItem:{} xData.item:{}", m_stItem.ShortDebugString(), xData.item().ShortDebugString());
        return ERR_CODE_INVALID_REQ_PARAM;
    }

    iRetCode = opMgr.AddToBag(m_stItem, m_stItem.GetNumber());
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "AddToBag m_stItem:{}", m_stItem.ShortDebugString());

    iRetCode = opMgr.Commit(true);
    CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "Commit");

    proto_ff::Proto_OnlineToLogicPickFromStorageReq req;
    *req.mutable_req() = xData.req();
    pPlayer->SendTransToOnlineServer(E_SS_ONLINE_ROLE_PICK_FROM_STORAGE, req, GetGlobalId(), 0);
    SetState(EN_OP_PICK_FROM_STORAGE_COST);
    return 0;
}

int NFTransOpStoreageBag::HandleStoragePickStorageRes(uint32_t nMsgId, const NFDataPackage& packet, uint32_t reqTransId, uint32_t rspTransId)
{
        int iRetCode = 0;
    CHECK_EXPR(GetState() == EN_OP_PICK_FROM_STORAGE_COST, -1, "HandleStoragePickStorageRes GetState:{} != EN_OP_PICK_FROM_STORAGE_COST", GetState());

    proto_ff::Proto_OnlineToLogicPickFromStorageRsp xData;
    CLIENT_MSG_PROCESS_WITH_PRINTF(packet, xData);

    auto pPlayer = GetPlayer();
    CHECK_NULL(0, pPlayer);

    if (xData.result() != 0)
    {
        auto pItemDesc = ItemDesc::Instance()->GetDesc(m_iItemId);
        CHECK_NULL_WF(pPlayer->GetRoleID(), pItemDesc, "item_id:{}", m_iItemId);

        NFBagPageOpMgr opMgr(pPlayer, proto_ff::EN_ICR_STORAGE_OP, true);

        NFBagPageOp* pBagPageOp = opMgr.GetBagPageOpByBagType(m_iBagPage);
        CHECK_NULL_WF(pPlayer->GetRoleID(), pBagPageOp, "GetBagPageOpByBagType, iBagType:{} iGridIndex:{}", m_iBagPage, m_iGridIndex);

        SelectItemGridParam selectItemGridParam;
        selectItemGridParam.base = m_stItem.GetItemBase();

        iRetCode = pBagPageOp->DelItem(selectItemGridParam, m_stItem.GetNumber());
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "DelItems, m_stItem:{}", m_stItem.ShortDebugString());

        iRetCode = opMgr.Commit(true);
        CHECK_ERR(pPlayer->GetRoleID(), iRetCode, "Commit");

        LOG_ERR(pPlayer->GetRoleID(), xData.result(), "HandleStoragePickStorageRes result:{}", xData.result());
        SetFinished(xData.result());
        return 0;
    }

    proto_ff::Proto_CSRsp respond;
    proto_ff::Proto_CSStorageMoveRsp* rsp = respond.mutable_storage_move_pick_resp();
    rsp->set_storage_page(m_iBagPage);
    rsp->set_grid_index(m_iGridIndex);
    NFPacketSendUtil::SendToClient(pPlayer, proto_ff::E_CS_STORAGE_PICK_RSP, &respond);
    SetFinished(0);
    return 0;
}
